/**
 * 
 */

//My All Global variable for this page here
var usr_profile_cache = null;

//First function is load function. Convention
function loadUserProfile(){
	if(usr_profile_cache == null){
		setup_userProfileCache(success_populateUserProfile); 
	} else{
		success_populateUserProfile();
	}
}

function setup_userProfileCache(handlerfunc){
	var dcmd = {
			callback : success_populateUserProfile,
			exceptionHandler : exception_populateUserProfile,
			timeout : 100000,
			errorHandler : exception_populateUserProfile
		};
	
	jQuery('#userProfile_activity_throbber').removeClass('throbberOff').addClass('throbberOn');
	DWR_Register.getUserProfile(userProfileIdCache,dcmd);
}


function success_populateUserProfile(dwrresp){
	
	jQuery('#userProfile_activity_throbber').removeClass('throbberOn').addClass('throbberOff');
	if (dwrresp && dwrresp.respCode == 0) {
		if (dwrresp.objRespOrError != null) {
			usr_profile_cache = dwrresp.objRespOrError;
			populateUserProfileFromCache();
			//dwr.util.setValue("password")
			
		}
	}
	
}

function populateUserProfileFromCache(){
	dwr.util.setValue("username",loggedinuser);// = loggedinuser;//usr_profile_cache.emailAddress;
	dwr.util.setValue("fname",usr_profile_cache.firstName);
	dwr.util.setValue("lname",usr_profile_cache.lastName);
	dwr.util.setValue("addr_line1",usr_profile_cache.addressLine1);
	dwr.util.setValue("addr_line2",usr_profile_cache.addressLine2);
	dwr.util.setValue("city",usr_profile_cache.city);
	dwr.util.setValue("state",usr_profile_cache.state) ;
	dwr.util.setValue("zip",usr_profile_cache.zip);
	dwr.util.setValue("phoneNumber",usr_profile_cache.workPhone);
	dwr.util.setValue("faxNumber",usr_profile_cache.fax) ;
	dwr.util.setValue("username_confirm",usr_profile_cache.emailAddress);
	dwr.util.setValue("country",usr_profile_cache.country);
	dwr.util.setValue("deliveryNote",usr_profile_cache.deliveryInfo);
}

function exception_populateUserProfile(){
	jQuery('#userProfile_activity_throbber').removeClass('throbberOn').addClass('throbberOff');
}

function updateUserProfile(){
	//console.log("Hi");
	//jQuery('#from_user_register')
	if(ketchupObj.allFieldsValidYSNOverride(jQuery('#from_user_register'),true)){
		var regXfer = {}
		regXfer.fname = dwr.util.getValue("fname");
		regXfer.lname = dwr.util.getValue("lname");
		regXfer.addressLine1 = dwr.util.getValue("addr_line1");
		regXfer.addressLine2 = dwr.util.getValue("addr_line2");
		regXfer.city = dwr.util.getValue("city");
		regXfer.state = dwr.util.getValue("state");
		regXfer.zip = dwr.util.getValue("zip");
		regXfer.workPhone = dwr.util.getValue("phoneNumber");
		regXfer.fax = dwr.util.getValue("faxNumber");
		regXfer.country = dwr.util.getValue("country");
		regXfer.deliveryInfo = dwr.util.getValue("deliveryNote");
		
		jQuery('#userProfile_activity_throbber').removeClass('throbberOff').addClass('throbberOn');
		var dwrCallMetaData = { 
				  callback:success_populateUserProfile, 
				  exceptionHandler: exception_populateUserProfile,
				  timeout: 100000,
				  errorHandler:  exception_populateUserProfile
		};
		DWR_Register.updateUser(regXfer,usr_profile_cache.userProfileId,dwrCallMetaData);
	}
	
}

function resetUserProfile(){
	//confirm message
	populateUserProfileFromCache();
}
